package com.nw.news.weather;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nw.news.R;
import com.nw.news.base.BaseActivity;
import com.nw.news.weather.entity.WeatherEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class WeatherActivity extends BaseActivity {

    String apiKey = "0edf7b5d61dac5718d582584b9598c4e";
    String apiUrl = "http://apis.juhe.cn/";

    String city = "北京";
    private TextView today;
    private TextView other;

    @Override
    public int getLayoutId() {
        return R.layout.activity_weather;
    }

    @Override
    public void init() {
        EditText edit = findViewById(R.id.edit);
        today = findViewById(R.id.today);
        other = findViewById(R.id.other);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String city = edit.getText().toString().trim();
                if (TextUtils.isEmpty(city)) {
                    Toast.makeText(WeatherActivity.this, "请输入城市名称", Toast.LENGTH_LONG).show();
                    return;
                }
                request(city);
            }
        });
        request(city);
    }

    public void request(String city) {
        //创建Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(apiUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建网络请求接口的实例
        ApiService apiService = retrofit.create(ApiService.class);
        //对发送送请求进行封装
        retrofit2.Call<WeatherEntity> call = apiService.getWeather(this.apiKey, city);
        //发送网络请求(异步)
        call.enqueue(new Callback<WeatherEntity>() {
            @Override
            public void onResponse(retrofit2.Call<WeatherEntity> call, Response<WeatherEntity> response) {
                try {
                    WeatherEntity entity = response.body();
                    Log.e("jjjjjj", "onResponse: " + entity.error_code + "=====" + Thread.currentThread().getName());
                    if (entity.error_code == 0) {
                        //请求成功
                        setDate(entity);
                    } else {
                        Toast.makeText(WeatherActivity.this, entity.reason, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {

                }
                //                mList = entity.result;
                //这里是子线程不能更新UI
                //                handler.sendEmptyMessage(10);
            }

            @Override
            public void onFailure(Call<WeatherEntity> call, Throwable t) {
                Log.i("onFailure", "请求失败");
            }
        });
    }

    private void setDate(WeatherEntity entity) {
        StringBuilder todayStr = new StringBuilder();
        todayStr.append("城市：");
        todayStr.append(entity.result.city).append("\n");
        todayStr.append("天气情况：" + entity.result.realtime.info).append("\n");
        todayStr.append("温度：" + entity.result.realtime.temperature).append("\n");
        todayStr.append("湿度：" + entity.result.realtime.humidity).append("\n");
        todayStr.append("风向：" + entity.result.realtime.direct).append("\n");
        todayStr.append("风力：" + entity.result.realtime.power).append("\n");
        todayStr.append("空气质量指数：" + entity.result.realtime.aqi).append("\n");

        today.setText(todayStr.toString());
        StringBuilder futureStr = new StringBuilder();
        for (int i = 0; i < entity.result.future.size(); i++) {
            futureStr.append("日期：" + entity.result.future.get(i).date).append("            ");
            futureStr.append("温度：" + entity.result.future.get(i).temperature).append("\n");
            futureStr.append("天气情况：" + entity.result.future.get(i).weather).append("            ");
            futureStr.append("风向：" + entity.result.future.get(i).direct).append("\n");
            futureStr.append("\n");
        }
        other.setText(futureStr.toString());
    }
}
